package com.example.iot.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.iot.DB.DatabaseHelper;
import com.example.iot.R;
import com.example.iot.utils.OkHttpUtils;
import com.example.iot.utils.ToastUtils;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
@BindView(R.id.iv_login_back1)
    ImageView myiv_back1;
@BindView(R.id.tv_login_forgive)
    TextView mytv_forgive;
@BindView(R.id.tv_login_register)
TextView mytv_register;
@BindView(R.id.et_login_username)
    EditText myet_username;
@BindView(R.id.et_login_password)
EditText myet_password;
@BindView(R.id.bt_login_login)
    Button mybt_login;
private ToastUtils toastUtils=new ToastUtils(this);
    DatabaseHelper dbHelper = new DatabaseHelper(LoginActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        //设置状态栏为透明
        Window window = getWindow();
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        window.setStatusBarColor(Color.TRANSPARENT);

        //绑定控件
        ButterKnife.bind(this);
        //为输入框设置文字监听，当输入框为空，登录按钮不可用
        TextChange textChange=new TextChange();
        myet_username.addTextChangedListener(textChange);
        myet_password.addTextChangedListener(textChange);
    }

    @OnClick(R.id.tv_login_register)
    public void register(){
        Intent i=new Intent(this, RegisterActivity.class);
        startActivity(i);
    }
    @OnClick(R.id.tv_login_forgive)
    public void forgive(){
        Intent i=new Intent(this, ForgivePasswordActivity.class);
        startActivity(i);
    }
    @OnClick(R.id.bt_login_login)
    public void login(){
        if(myet_username.length()!=0&&myet_password.length()!=0){
            String username=myet_username.getText().toString();
            String password=myet_password.getText().toString();
            OkHttpUtils.posyAsync("http://42.81.143.229:8091/login", username, password, new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    Log.e("uuu","iiiiiiiiii");
                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    if(response.isSuccessful()){

                        Log.e("pot异步请求", "postASync:"+response.body().string()+response.code());
                        SQLiteDatabase db = dbHelper.getWritableDatabase();
                        ContentValues values = new ContentValues();
                        values.put("username",username);
                        values.put("password", password);
                        db.insert("user", null, values);
                        db.close();
                    }
                }
            });

            /*okHttpClient = new OkHttpClient();
            RequestBody muiltipartBody = new MultipartBody.Builder()
                    //一定要设置这句
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("username", myet_username.getText().toString())
                    .addFormDataPart("password", myet_password.getText().toString())
                    .build();
            //FormBody formBody = new FormBody.Builder().add("username", "yt").add("password", "yt1234").build();
           // RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=utf-8"), "select * from user");
            Request request = new Request.Builder().url("http://42.81.143.229:8091/login")
                    .post(muiltipartBody)
                    .addHeader("token","test")
                    .build();
            Call call=okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    Log.e("uuu","iiiiiiiiii");
                }
                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                }
            });*/
        }
    }

private class TextChange implements TextWatcher{
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if(myet_username.length()!=0&&myet_password.length()!=0){
            mybt_login.setEnabled(true);
        }else{mybt_login.setEnabled(false);}
    }
}

}